package com.dyw.demoprovider.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyw.demoapi.dto.MailDTO;
import com.dyw.demoapi.entity.MailUser;
import com.dyw.demoapi.service.MailUserService;
import com.dyw.demoprovider.service.mapper.UserMapper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.cuit.epoch.result.R;
import org.cuit.epoch.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Devil
 * @create 2022-03-05 14:44
 */
@Log4j2
@Service
@DubboService
public class MailUserServiceImpl implements MailUserService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    UserMapper userMapper;

    @Override
    public Result save(MailUser mailUser) {
        userMapper.insert(mailUser);
        return R.success();
    }

    @Override
    public Result<MailUser> selectUserById(Integer id) {
        LambdaQueryWrapper<MailUser> mailUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        mailUserLambdaQueryWrapper.eq(MailUser::getId, id).last("last 1");
        MailUser mailUser = userMapper.selectOne(mailUserLambdaQueryWrapper);
        return R.success(mailUser);
    }

    @Override
    public Result<MailUser> selectUserByInfo(MailUser mailUser) {
        MailUser mailUser1 = userMapper.selectById(mailUser);
        return R.success(mailUser1);
    }

    @Override
    public Result<List<MailUser>> selectUserList() {
        List<MailUser> mailUsers = userMapper.selectList(new LambdaQueryWrapper<>());
        return R.success(mailUsers);
    }

    @Override
    public Result<String> sendMail(MailDTO mailDTO) {
        List<Integer> idList = mailDTO.getId();
        String sendTime = mailDTO.getSendTime();
        String subject = mailDTO.getSubject();
        String content = mailDTO.getContent();
        List<MailUser> userList = null;

        if (StringUtils.isBlank(subject) || StringUtils.isBlank(content)) {
            return R.fail("参数有误");
        }
        if (idList.size() <= 0) {
            userList = userMapper.selectList(new LambdaQueryWrapper<>());
        } else {
            userList = userMapper.selectBatchIds(idList);
        }

        if (StringUtils.isBlank(sendTime)) {
            sendTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        }
        mailDTO.setUserList(userList);
        mailDTO.setSendTime(sendTime);

        rocketMQTemplate.asyncSend("notice:mail", mailDTO, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("发送信息成功");
            }

            @Override
            public void onException(Throwable e) {
                log.error("发送失败");
            }
        });
        return R.success();
    }
}
